package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.BaseSalary;
import cn.edu.cdu.group4nb.service.DoSalaryService;
import cn.edu.cdu.group4nb.utils.MakeExcel;
import cn.edu.cdu.group4nb.utils.R;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SalaryController
 * Package: cn.edu.cdu.group4nb.controller
 *
 * @date: 2019-10-09 20:11
 * @author:timexin
 */
@Controller
public class SalaryController {

    @Autowired
    private DoSalaryService doSalaryService;

    @ResponseBody
    @RequestMapping(value = "/Salary/ViewSalary" )
    public List<Map<String,Object>> ViewSalary(){
        return doSalaryService.ViewSalary();
    }

    @RequestMapping(value = "/Salary/SetBaseSalary")
    @ResponseBody
    public R SetSalary(@RequestBody Map<String,Object> map){

        doSalaryService.SetBaseSalary(map);
        return R.ok();

    }
    @RequestMapping(value = "/Salary/BSalary")
    @ResponseBody
    public List<BaseSalary> SeeSalary(){

        return doSalaryService.SelectSalary();
    }


    @ResponseBody
    @RequestMapping(value="/Salary/ManagerSalary")
    public List<Map<String,Object>> PutSalary(){
        return doSalaryService.UserSalary();
    }

    //导出Excel表
    @RequestMapping("/Salary/ExcelSalary")
    public void ExcelSalary( HttpServletResponse response){
        //获取数据
        List<Map<String,Object>> maps= doSalaryService.UserSalary();
        //excel的标题
        String[] title = {"用户ID","姓名","岗位","底薪","奖金","薪资总和"};
        //下载后文件的名称
        String fileName = "薪资总和表.xls";
        //sheet 名
        String sheetName = "薪资记录表";
        //建一个二维数组，前面放行，后面放列
        String[][] content = new String[maps.size()][title.length];

        for(int i=0;i<maps.size();i++) {
            Map<String, Object> map = maps.get(i);
            content[i][0] = MakeExcel.IsNull(map.get("userid"));  // map.get("userid").toString();
            content[i][1] = MakeExcel.IsNull(map.get("name"));     //map.get("name").toString();
            content[i][2] = MakeExcel.IsNull(map.get("posname"));      //  map.get("posname").toString();
            content[i][3] = MakeExcel.IsNull(map.get("salary")); // map.get("salary").toString();
            if ((MakeExcel.IsNull(map.get("attendence"))).equals("1")) {
                content[i][4] = "200";
                if(MakeExcel.IsNull(map.get("salary")).equals(" ")){
                    content[i][5]  ="暂时未分配薪资";
                }else {
                int a = (int)map.get("salary")+200;
                content[i][5] = String.valueOf(a);
                }
            } else {
                content[i][4] = MakeExcel.IsNull(map.get("attendence"));  //map.get("attendence").toString();
                content[i][5] = MakeExcel.IsNull(map.get("salary"));
            }
             }



            HSSFWorkbook wb = MakeExcel.getExcel(sheetName,title,content);

            //响应到客户端
            try {
                MakeExcel.setResponseHeader(response,fileName);
                OutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


    }
}
